//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.core.config;

import com.ruoyi.common.config.RuoYiConfig;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private RuoYiConfig ruoyiConfig;
    @Value("${swagger.enabled}")
    private boolean enabled;
    @Value("${swagger.pathMapping}")
    private String pathMapping;

    public SwaggerConfig() {
    }

    @Bean
    public Docket createRestApi() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .pathMapping(this.pathMapping);
    }

    @Bean
    public Docket webApiClient() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/client/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("客户维护")
                .pathMapping(this.pathMapping);
    }

    @Bean
    public Docket webApiProject() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/project/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("项目管理")
                .pathMapping(this.pathMapping);
    }

    /**
     * 工作流分组
     */
    @Bean
    public Docket packetWebApiActiviti() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/flowdata/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("工作流")
                .pathMapping(this.pathMapping);
    }

    /**
     * 财报信息分组
     */
    @Bean
    public Docket packetWebFinancingReport() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/client/financingreport/"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("财报信息")
                .pathMapping(this.pathMapping);
    }

    /**
     * 报价测算分组
     */
    @Bean
    public Docket packetWebQuoted() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/quoted/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("报价测算")
                .pathMapping(this.pathMapping);
    }

    /**
     * 合同管理分组
     */
    @Bean
    public Docket packetWebContract() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/contract/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("合同管理")
                .pathMapping(this.pathMapping);
    }

    /**
     * 档案管理分组
     */
    @Bean
    public Docket packetWebAfterRentFile() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/afterRentFile/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("档案管理")
                .pathMapping(this.pathMapping);
    }

    /**
     * 租后检查分组
     */
    @Bean
    public Docket packetWebAfterRent() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/afterRent/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("租后检查")
                .pathMapping(this.pathMapping);
    }

    /**
     * 租金管理分组
     */
    @Bean
    public Docket packetWebRent() {
        return (new Docket(DocumentationType.SWAGGER_2))
                .enable(this.enabled)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/rent/**"))
//                .paths(PathSelectors.ant("/historyFromData/**"))
//                .paths(PathSelectors.ant("/activitiHistory/**"))
                .build()
                .securitySchemes(this.securitySchemes())
                .securityContexts(this.securityContexts())
                .groupName("租金管理")
                .pathMapping(this.pathMapping);
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList();
        securityContexts.add(SecurityContext.builder().securityReferences(this.defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
        List<SecurityReference> securityReferences = new ArrayList();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    private ApiInfo apiInfo() {
        return (new ApiInfoBuilder()).title("标题：融资租赁管理系统_接口文档").description("描述：用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...").contact(new Contact(this.ruoyiConfig.getName(), (String)null, (String)null)).version("版本号:" + this.ruoyiConfig.getVersion()).build();
    }
}
